from app import db
from datetime import datetime

class Supplier(db.Model):
    """供应商表"""
    __tablename__ = 'supplier'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    code = db.Column(db.String(50), unique=True, nullable=False, comment='供应商编码')
    name = db.Column(db.String(100), nullable=False, comment='供应商名称')
    contact_person = db.Column(db.String(50), comment='联系人')
    phone = db.Column(db.String(20), comment='联系电话')
    email = db.Column(db.String(100), comment='邮箱')
    address = db.Column(db.String(200), comment='地址')
    bank_name = db.Column(db.String(100), comment='开户银行')
    bank_account = db.Column(db.String(50), comment='银行账户')
    tax_number = db.Column(db.String(50), comment='税号')
    status = db.Column(db.Enum('ACTIVE', 'INACTIVE'), default='ACTIVE', comment='状态')
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
    is_deleted = db.Column(db.Boolean, default=False)
    
    def to_dict(self):
        return {
            'id': self.id,
            'code': self.code,
            'name': self.name,
            'contact_person': self.contact_person,
            'phone': self.phone,
            'email': self.email,
            'address': self.address,
            'bank_name': self.bank_name,
            'bank_account': self.bank_account,
            'tax_number': self.tax_number,
            'status': self.status,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None,
            'is_deleted': self.is_deleted
        }

class PurchaseOrder(db.Model):
    """采购订单表"""
    __tablename__ = 'purchase_order'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    order_no = db.Column(db.String(50), unique=True, nullable=False, comment='采购订单号')
    supplier_id = db.Column(db.BigInteger, db.ForeignKey('supplier.id'), nullable=False, comment='供应商ID')
    order_date = db.Column(db.Date, nullable=False, comment='订单日期')
    delivery_date = db.Column(db.Date, comment='预计交货日期')
    total_amount = db.Column(db.Numeric(15, 2), comment='总金额')
    status = db.Column(db.Enum('DRAFT', 'APPROVED', 'PARTIAL_RECEIVED', 'RECEIVED', 'CANCELLED'), 
                       default='DRAFT', comment='状态')
    created_by = db.Column(db.BigInteger, nullable=False, comment='创建人')
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
    is_deleted = db.Column(db.Boolean, default=False)
    
    # 关联供应商
    supplier = db.relationship('Supplier', backref='purchase_orders')
    
    def to_dict(self):
        return {
            'id': self.id,
            'order_no': self.order_no,
            'supplier_id': self.supplier_id,
            'order_date': self.order_date.isoformat() if self.order_date else None,
            'delivery_date': self.delivery_date.isoformat() if self.delivery_date else None,
            'total_amount': float(self.total_amount) if self.total_amount else 0.0,
            'status': self.status,
            'created_by': self.created_by,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None,
            'is_deleted': self.is_deleted,
            'supplier': self.supplier.to_dict() if self.supplier else None
        }

class PurchaseOrderItem(db.Model):
    """采购订单明细表"""
    __tablename__ = 'purchase_order_item'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    order_id = db.Column(db.BigInteger, db.ForeignKey('purchase_order.id'), nullable=False, comment='订单ID')
    product_id = db.Column(db.BigInteger, db.ForeignKey('product.id'), nullable=False, comment='产品ID')
    quantity = db.Column(db.Numeric(15, 6), nullable=False, comment='数量')
    unit_price = db.Column(db.Numeric(15, 6), nullable=False, comment='单价')
    amount = db.Column(db.Numeric(15, 2), comment='金额')
    received_quantity = db.Column(db.Numeric(15, 6), default=0, comment='已收货数量')
    unit = db.Column(db.String(20), comment='单位')
    
    # 关联订单和产品
    order = db.relationship('PurchaseOrder', backref='items')
    product = db.relationship('Product', backref='purchase_order_items')
    
    def to_dict(self):
        return {
            'id': self.id,
            'order_id': self.order_id,
            'product_id': self.product_id,
            'quantity': float(self.quantity) if self.quantity else 0.0,
            'unit_price': float(self.unit_price) if self.unit_price else 0.0,
            'amount': float(self.amount) if self.amount else 0.0,
            'received_quantity': float(self.received_quantity) if self.received_quantity else 0.0,
            'unit': self.unit,
            'order': self.order.to_dict() if self.order else None,
            'product': self.product.to_dict() if self.product else None
        }